package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.dto.SysUserInfo;
import com.itheima.ydd.entity.Role;
import com.itheima.ydd.entity.User;
import com.itheima.ydd.mapper.UserMapper;
import com.itheima.ydd.service.RoleService;
import com.itheima.ydd.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @since 2022-02-18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private RoleService roleService;

    /**
     * 用户添加
     *
     * @param sysUserInfo
     */
    @Override
    @Transactional
    public void add(SysUserInfo sysUserInfo) {
        User user = getUser(sysUserInfo);
        this.save(user);
    }

    /**
     * 管理员修改
     *
     * @param sysUserInfo
     */
    @Override
    @Transactional
    public void updateUser(SysUserInfo sysUserInfo) {
        User user = getUser(sysUserInfo);
        this.updateById(user);
    }

    /**
     * 分页查询
     *
     * @param login_name
     * @param phone
     * @param status
     * @param page
     * @param limit
     * @return
     */
    @Override
    public HashMap<String, Object> getPageHashMap(String login_name, String phone, String status, Integer page, Integer limit) {
        Page<User> userPage = new Page<>(page, limit);
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.like(login_name != null, User::getLogin_name, login_name);
        lqw.like(phone != null, User::getPhone, phone);
        lqw.like(status != null, User::getStatus, status);
        this.page(userPage, lqw);
        List<User> records = userPage.getRecords();
        records.forEach(item -> {
            String[] ids = item.getIds();
            List<Object> releName = roleService.listObjs(new LambdaQueryWrapper<Role>().select(Role::getName).in(Role::getId, ids));
            item.setRolename(releName.toArray(new String[0]));
        });
        Integer count = this.count(lqw);
        HashMap<String, Object> map = new HashMap<>();
        map.put("count", count);
        map.put("currentPage", page);
        map.put("pageSize", limit);
        map.put("totalPages", userPage.getTotal());
        map.put("data", records);
        return map;
    }

    /**
     * 装配User对象
     *
     * @param sysUserInfo
     * @return
     */
    private User getUser(SysUserInfo sysUserInfo) {
        User user = new User();
        //copyProperties用于复制类的同名属性
        BeanUtils.copyProperties(sysUserInfo, user);
        List<String> role_id = sysUserInfo.getRole_id();
        //使用StringBuffer效率比String高且线程安全
        StringBuffer s = new StringBuffer("[");
        for (int i = 0; i < role_id.size(); i++) {
            if (i == role_id.size() - 1) {
                s.append("\"").append(role_id.get(i)).append("\"");
            } else {
                s.append("\"").append(role_id.get(i)).append("\",");
            }
        }
        s.append("]");
        user.setRole_id(s.toString());
        return user;
    }


}
